<template>
    <van-datetime-picker @confirm="onConfirm" @cancel="$emit('close')" v-model="currentDate" type="date" title="选择年月日"
        :min-date="minDate" :max-date="maxDate" />
</template>

<script>
import { upUserProfileAPI } from '@/api/user'
import { ref, inject } from 'vue'
import { Toast } from 'vant'
export default {
  name: 'editBirthday',
  props: {
    birthday: {
      type: String,
      required: true
    }
  },
  setup (props, content) {
    // 日期格式化
    const currentDate = ref(new Date(props.birthday))
    const dayJS = inject('dayJS')
    const onConfirm = async () => {
      Toast.loading({
        duration: 0,
        forbidClick: true,
        message: '修改中...'
      })
      // 转化成后端接受的日期格式
      const loacalBirthday = dayJS(currentDate.value).format('YYYY-MM-DD')
      try {
        await upUserProfileAPI({
          birthday: loacalBirthday
        })
        // 修改父组件本地数据
        content.emit('upBirthday', loacalBirthday)
        // 关闭弹层
        content.emit('close')
        // 显示成功
        Toast.success('修改成功')
      } catch (err) {
        console.log(err)
      }
    }

    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate,
      onConfirm
    }
  }
}
</script>

<style scoped lang="less">

</style>
